﻿using System;
using System.Linq.Expressions;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Reflection;
using System.Collections.ObjectModel;
using EmbeddedLanguage.SubProcessors;
using EmbeddedLanguage.Attributes;

namespace EmbeddedLanguage
{
    [JSNamespaceSubstition(Namespace = ObjectEx.RootNS)]
    public static class Core<T>
    {

        static Core()
        {
            //__Core.Compile().Invoke(null);
            IntLoopSearch = IntLoopSearch_expr.Compile();
        }

        #region Register Static Funcs
        private static Expression<Action<Object>> __Core = C => C
            .AssignStaticFunc_1x1(out ExecFunc, ExecFunc_expr)
            //.AssignStaticFunc_1x1(out TestForPrime3, TestForPrime3_expr)
        ;
        #endregion

        #region IntLoopSearch
        public static Expression<Func<int, int, Func<int, bool>, Func<int, T>, Func<T>, T>> IntLoopSearch_expr =
            (Start, End, Test, EvalFn, EvalIfNotFound) =>
                Start.UpTo(End, new Range<T>.RangeActionList
                {
                    rng => rng.If(
                                
                                rng_ => Test(rng_.Val), 
                                rng_ => rng_.Assign(out rng_.ReturnObj,  EvalFn(rng_.Val)).Break(),
                                rng_ => rng_.Assign(out rng_.ReturnObj, EvalIfNotFound())
                           )
                }).ReturnObj;   //.DoUntilTestAndEvaVal(Test, EvalFn, EvalIfNotFound);
        
        [SubProcessor(
            JSSubProcessorType = typeof(IntLoopSearchJSProcessor)
        )]
        public static Func<int, int, Func<int, bool>, Func<int, T>, Func<T>, T> IntLoopSearch;

        
        #endregion

        

        [JSExplicitScript(
            Script=@"function (fn) { return fn(); }"
        )]
        public static Expression<Func<Func<T>, T>> ExecFunc_expr = createNewFn => createNewFn();

       
        public static Func<Func<T>, T> ExecFunc;

        

        

    }

    
    
}
